{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'matplotlib'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-ba09192b13e6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mN\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'matplotlib'"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "k = 1\n",
    "N = 15\n",
    "K_p = 0.19\n",
    "K_i = 0.0178\n",
    "K_0 = 1\n",
    "\n",
    "# 1000个0的集合list\n",
    "input_signal = np.zeros(1000)\n",
    "\n",
    "integrator_out = 0\n",
    "\n",
    "phase_estimate = np.zeros(1000)\n",
    "\n",
    "e_D = []  # phase-error output\n",
    "e_F = []  # loop filter output\n",
    "# 1000个0的集合\n",
    "sin_out = np.zeros(1000)\n",
    "# 1000个1的集合\n",
    "cos_out = np.ones(1000)\n",
    "# 1000次循环\n",
    "for n in range(999):\n",
    "    # 设置个输入余弦信号\n",
    "    input_signal[n] = np.cos(2*np.pi*(k/N)*n + np.pi)\n",
    "\n",
    "\n",
    "    # phase detector\n",
    "    try:\n",
    "        e_D.append(input_signal[n] * sin_out[n])\n",
    "    except IndexError:\n",
    "        e_D.append(0)\n",
    "    print(input_signal[n]*sin_out[n])\n",
    "\n",
    "    #loop filter\n",
    "    integrator_out += K_i * e_D[n]\n",
    "    e_F.append(K_p * e_D[n] + integrator_out)\n",
    "\n",
    "    #NCO\n",
    "    try:\n",
    "        phase_estimate[n+1] = phase_estimate[n] + K_0 * e_F[n]\n",
    "    except IndexError:\n",
    "        phase_estimate[n+1] = K_0 * e_F[n]\n",
    "\n",
    "    sin_out[n+1] = -np.sin(2*np.pi*(k/N)*(n+1) + phase_estimate[n])\n",
    "    cos_out[n+1] = np.cos(2*np.pi*(k/N)*(n+1) + phase_estimate[n])\n",
    "\n",
    "\n",
    "# Create a Figure\n",
    "fig = plt.figure()\n",
    "\n",
    "# Set up Axes\n",
    "ax1 = fig.add_subplot(211)\n",
    "ax1.plot(cos_out, label='PLL Output')\n",
    "plt.grid()\n",
    "ax1.plot(input_signal, label='Input Signal')\n",
    "plt.legend()\n",
    "ax1.set_title('Waveforms')\n",
    "\n",
    "# Show the plot\n",
    "# plt.show()\n",
    "\n",
    "ax2 = fig.add_subplot(212)\n",
    "ax2.plot(e_F)\n",
    "plt.grid()\n",
    "ax2.set_title('Filtered Error')\n",
    "plt.show()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
